//
//  NewsDTCell.m
//  suRead
//
//  Created by lidi on 2018/1/30.
//  Copyright © 2018年 bao. All rights reserved.
//

#import "NewsDTCell.h"
#import "M_News.h"
#import "AccountManager.h"
#import "UIImageView+WebCache.h"
#import "NewCellManager.h"

@interface NewsDTCell()
@property (weak, nonatomic) IBOutlet UILabel *labelViewCount;
@property (strong, nonatomic) IBOutlet UILabel *zanLable;
@property (weak, nonatomic) IBOutlet UIView *viewCountContainer;
@property (weak, nonatomic) IBOutlet UIImageView *imageViewIcon;
@property (weak, nonatomic) IBOutlet UILabel *labelTitle;
@property (weak, nonatomic) IBOutlet UILabel *labelCreateTime;
@property (weak, nonatomic) IBOutlet UIImageView *imageViewFlag;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *flagWidthConstraint;
@end

@implementation NewsDTCell

- (void)awakeFromNib {
    [super awakeFromNib];
    if ( [[[UIDevice currentDevice]systemVersion] floatValue] >= 8.0) {
        self.layoutMargins = UIEdgeInsetsZero;
        self.preservesSuperviewLayoutMargins = NO;
        self.separatorInset = UIEdgeInsetsMake(0, 15.0f, 0, 15.0f);
    }
}

- (void)setNewsData:(id)newsData
{
    if ([newsData isKindOfClass:[M_News class]])
    {
        M_News *news = (M_News *)newsData;
        
        NSString *imageURL = news.f_thumbnail;
        [self.imageViewIcon sd_setImageWithURL:[NSURL URLWithString:imageURL] placeholderImage:[UIImage imageNamed:@"lazy"]];
        
        NSString *title = news.f_title;
        self.labelTitle.text = title;
        
        //已读未读
        if ([[NewCellManager shared] hasRead:news.f_id]) {
            self.labelTitle.textColor = [UIColor lightGrayColor];
        } else {
            self.labelTitle.textColor = [UIColor whiteColor];
        }
        
        NSString *createTime = news.f_updateTime;
        self.labelCreateTime.text = createTime;
        
        if ([AccountManager sharedAccountManager].f_isShowViewCount) {
            self.viewCountContainer.hidden = NO;
            self.labelViewCount.text = news.f_views;
            [self.zanLable setText:[NSString stringWithFormat:@"%ld",news.f_square]];
        } else {
            self.viewCountContainer.hidden = YES;
        }
        
        /*
         置顶|99
         最新|1
         实发|2
         热点|3
         独家|4
         专题|5
         推广|6
         视频|7
         直播|8
         福利9
         */
        self.flagWidthConstraint.constant = 36.0f;
        NSString *flagName = @"";
        switch ([news.f_newsFlag intValue]) {
            case 99:
                flagName = @"news_flag_01";
                break;
            case 1:
                flagName = @"news_flag_02";
                break;
            case 2:
                flagName = @"news_flag_03";
                break;
            case 3:
                flagName = @"news_flag_04";
                break;
            case 4:
                flagName = @"news_flag_05";
                break;
            case 5:
                flagName = @"news_flag_06";
                break;
            case 6:
                flagName = @"news_flag_07";
                break;
            case 7:
                flagName = @"news_flag_08";
                break;
            case 8:
                flagName = @"news_flag_09";
                break;
            case 9:
                flagName = @"news_flag_10";
                break;
            default:
                flagName = @"icon_time";
                self.flagWidthConstraint.constant = 15.0f;
                break;
        }
        self.imageViewFlag.image = [UIImage imageNamed:flagName];
    }
}

@end
